package com.daocao.jy.fragment;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.daocao.jy.R;
import com.daocao.jy.activity.ChatCenterActivity;
import com.daocao.jy.activity.InviteFriendActivity;
import com.daocao.jy.activity.MyCouponActivity;
import com.daocao.jy.activity.MyMessageActivity;
import com.daocao.jy.activity.MyOilCardActivity;
import com.daocao.jy.activity.MyOrderActivity;
import com.daocao.jy.activity.MyWalletActivity;
import com.daocao.jy.activity.PersonalCenterActivity;
import com.daocao.jy.activity.ReturnCashActivity;
import com.daocao.jy.activity.SettingActivity;
import com.daocao.jy.activity.VipPacketActivity;
import com.daocao.jy.api.Api;
import com.daocao.jy.constant.UserInfoGlobal;
import com.daocao.jy.util.Network;
import com.daocao.jy.util.RxLifeUtil;
import com.daocao.jy.util.Spans;
import com.daocao.jy.util.ToastUtil;
import com.daocao.jy.util.Util;
import com.daocao.jy.view.LoginDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-4-4.
 */

public class MainMyFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R.id.txt_return_money)
    TextView txtReturnMoney;
    @BindView(R.id.txt_add_money)
    TextView txtAddMoney;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.btn_login)
    TextView btnLogin;
    @BindView(R.id.txt_name)
    TextView txtName;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_main_my;
    }

    @Override
    protected void initView() {
        int textSize36 = getResources().getDimensionPixelSize(R.dimen.x36);
        int textSize24 = getResources().getDimensionPixelSize(R.dimen.x24);
        Spanned textOne = Spans.with(this).font("3000.00\n", textSize36, 0xffFB5E01).style(Typeface.BOLD).
                font("已返现金额(元)\n", textSize24, 0xffA6A6A6).
                build();
        txtReturnMoney.setText(textOne);

        Spanned textTwo = Spans.with(this).font("600.00\n", textSize36, 0xffFB5E01).style(Typeface.BOLD).
                font("累计佣金额(元)\n", textSize24, 0xffA6A6A6).
                build();
        txtAddMoney.setText(textTwo);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getUserInfo();
            }
        });
    }

    public void setLogout() {
        btnLogin.setVisibility(View.VISIBLE);
        txtName.setText("");
    }

    public void getUserInfo() {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }
        refresh.setRefreshing(true);
        Api.api_service.getUserInfo().compose(RxLifeUtil.NocheckOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.getStatus().equals("00")) {
                        btnLogin.setVisibility(View.GONE);
                        UserInfoGlobal.saveUserInfo(result.datas);
                        if (!TextUtils.isEmpty(result.datas.username)) {
                            txtName.setText(result.datas.username);
                            return;
                        }
                        txtName.setText(result.datas.phone);
                    } else {
                        txtName.setText("");
                        btnLogin.setVisibility(View.VISIBLE);
                    }

                });
    }

    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public boolean getIsLogin() {
        if (UserInfoGlobal.isLogin()) {
            return true;
        } else {
            new LoginDialog(getContext()).setDialogClickListener(new LoginDialog.DialogDefaultClickListener() {
                @Override
                public void loginSuccess() {
                    getUserInfo();
                }

                @Override
                public void cancel() {

                }
            }).show();
            return false;
        }
    }

    @OnClick({R.id.btn_order, R.id.btn_vip, R.id.img_icon, R.id.btn_login, R.id.btn_my_setting, R.id.btn_my_wallet, R.id.btn_my_oil, R.id.btn_my_youhui, R.id.btn_my_msg, R.id.btn_my_fanxian, R.id.btn_my_share, R.id.btn_my_chat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_order:
                Util.startActivity(getActivity(), MyOrderActivity.class);
                break;
            case R.id.btn_vip:
                Util.startActivity(getActivity(), VipPacketActivity.class);
                break;
            case R.id.img_icon:
                if (getIsLogin())
                    Util.startActivity(getActivity(), PersonalCenterActivity.class);
                break;
            case R.id.btn_login:
                new LoginDialog(getActivity()).show();
                break;
            case R.id.btn_my_setting:
                Util.startActivity(getActivity(), SettingActivity.class);
                break;

            case R.id.btn_my_wallet:
                if (getIsLogin())
                    Util.startActivity(getActivity(), MyWalletActivity.class);
                break;
            case R.id.btn_my_oil:
                if (getIsLogin())
                    Util.startActivity(getActivity(), MyOilCardActivity.class);
                break;
            case R.id.btn_my_youhui:
                if (getIsLogin())
                    Util.startActivity(getActivity(), MyCouponActivity.class);
                break;
            case R.id.btn_my_msg:
                Util.startActivity(getActivity(), MyMessageActivity.class);
                break;
            case R.id.btn_my_fanxian:
                if (getIsLogin())
                    Util.startActivity(getActivity(), ReturnCashActivity.class);
                break;
            case R.id.btn_my_share:
                Util.startActivity(getActivity(), InviteFriendActivity.class);
                break;
            case R.id.btn_my_chat:
                Util.startActivity(getActivity(), ChatCenterActivity.class);
                break;

        }
    }
}
